import shaHash from "crypto";
import rsaSign from "../../common/rsa.js";
import CryptoJS from "../../common/CryptoJS.js";
import appconf from "../../common/appconf.js";
const domain = appconf.domain;
import getDateTime from './getdateTime.js';

export default {
	data() {
		return {
			MenDianList: [],
			yesDay: {},
			ToDay: {},
			TodayOrder: 0,
			ToDayMoney: {
				profit: 0,
				totalNum: 0,
				turnover: 0,
			},
			YesDayMoney: {
				profit: 0,
				totalNum: 0,
				turnover: 0,
			},
			SelectMenDian: "", // 当前选择的门店，为门店id，
			ourApi:false,
			Permission: ""
		}
	},


	onShow() {
		this.$api.OurUpLoadUrl == 'https://jkht.wteam.club' ? this.ourApi = true:this.ourApi = false
		
		this.TodayOrder = 0
		this.ToDayMoney = {
			profit: 0,
			totalNum: 0,
			turnover: 0,
		}
		this.YesDayMoney = {
			profit: 0,
			totalNum: 0,
			turnover: 0,
		}
		this.MenDianList = []

		let token = uni.getStorageSync("token")
		// let curRoute  = this.$mp.page.route; // 直接获取当前页面路由
		// console.log(curRoute)
		if (!token) {
			//不存在则跳转至登录页
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}
		this.yesDay = getDateTime.getDateStr(null, -1); //昨天
		this.ToDay = getDateTime.getDateStr(null, 0); //今天

		this.storeQuery()

		this.getPermission()

	},
	/*监听底部上拉事件*/
	onReachBottom() {
		// 由于项目需求，现在暂不使用分页
		// if (this.noMore == false) {
		// 	this.pages++;
		// 	this.getPageData();
		// }
	},
	methods: {
		getPermission() {
		
			this.$request(this.$api.getPermission, null, "POST", false).then(res => {
				if (res.code == '0') {
					this.Permission = res.data
					uni.setStorageSync("permission", res.data)
		
				} else {
					uni.showToast({
						title: "错误",
						icon: 'error'
					});
				}
			})
		},
		toMonitorList() {
			uni.navigateTo({
				url: "/pages/monitorH5/monitorH5"
			})
		},
		toXiaoLiang(){
			uni.navigateTo({
				url: "/pages/XiaoLiang/XiaoLiang"
			})
		},
		toOrder(){
			uni.navigateTo({
				url: "/pages/order/newOrder"
			})
		},
		toBaoJin() {
			uni.navigateTo({
				url: "/pages/BaoJin/Index"
			})
		},
		towallet() {
			// uni.navigateTo({
			// 	url: "/pages/wallet/recharge/recharge"
			// })
			uni.navigateTo({
				url: "/pages/wallet/wallet"
			})
		},

		change(e) { // 用户选择门店
			wx.setStorageSync("SelectMenDian", this.SelectMenDian)
			this.TodayOrder = 0


			this.orderQueryByDate()
			this.turnoverQueryByDate('1')
			this.turnoverQueryByDate('2')
		},

		storeQuery() {
			this.$request(this.$api.storeQuery, null, "POST", false).then(res => {
				if (res.code == '0') {
					wx.setStorageSync("MenDianList", JSON.stringify(res.data))
					res.data.forEach(i => {
						let a = {
							text: i.name,
							name: i.id,
							value: i.id,
						}
						this.MenDianList.push(a)
					})
					this.IsMenDian(res.data)
				} else {
					uni.showToast({
						title: "错误",
						icon: 'error'
					});
				}
			})
		},
		IsMenDian(List) { // 判断当前选择的门店
			if (wx.getStorageSync("SelectMenDian") != '') {
				this.SelectMenDian = wx.getStorageSync("SelectMenDian") // 从缓存获取当前选择门店
			} else {
				this.SelectMenDian = List[0].id // 默认第一个门店
				wx.setStorageSync("SelectMenDian", this.SelectMenDian)
			}
			this.orderQueryByDate()
			this.turnoverQueryByDate('1')
			this.turnoverQueryByDate('2')
		},
		orderQueryByDate() { // 今日订单量         
			let data = {
				"day": this.ToDay.d,
				"month": this.ToDay.m,
				"year": this.ToDay.y,
				"storeId": this.SelectMenDian,
				"orderStatus": 1,
			}
			this.$request(this.$api.orderQueryByDate, data, "POST", false).then(res => {
				if (res.code == '0') { // 该门店今天有订单
					this.TodayOrder = res.data.total
				}
			})
		},
		turnoverQueryByDate(num) { // 营业额
			let data;
			if (num == '1') { // 今日营业额
				data = {
					"day": this.ToDay.d,
					"month": this.ToDay.m,
					"storeId": this.SelectMenDian,
					"year": this.ToDay.y,
				}
			}
			if (num == '2') { // 昨日营业额
				data = {
					"day": this.yesDay.d,
					"month": this.yesDay.m,
					"storeId": this.SelectMenDian,
					"year": this.yesDay.y,
				}
			}
			this.$request(this.$api.turnoverQueryByDate, data, "POST", false).then(res => {
				if (res.code == '0') { // 该门店今天有营业额
					// this.TodayOrder += res.data.length

					if (num == '1') { // 今日营业额
						this.ToDayMoney = {
							profit: 0,
							totalNum: 0,
							turnover: 0,
						}
						this.ToDayMoney.turnover += Number(res.data.turnover)
						this.ToDayMoney.turnover = this.ToDayMoney.turnover.toFixed(2)

						this.ToDayMoney.profit += Number(res.data.profit)
						this.ToDayMoney.profit = this.ToDayMoney.profit.toFixed(2)

						this.ToDayMoney.totalNum += Number(res.data.totalNum)
					}
					if (num == '2') { // 昨日营业额
						this.YesDayMoney = {
							profit: 0,
							totalNum: 0,
							turnover: 0,
						}

						this.YesDayMoney.turnover += Number(res.data.turnover)
						this.YesDayMoney.turnover = this.YesDayMoney.turnover.toFixed(2)

						this.YesDayMoney.profit += Number(res.data.profit)
						this.YesDayMoney.profit = this.YesDayMoney.profit.toFixed(2)

						this.YesDayMoney.totalNum += Number(res.data.totalNum)
					}

					this.$forceUpdate()
				}
			})

		},
		getTime: function() {
			var date = new Date(),
				year = date.getFullYear(),
				month = date.getMonth() + 1,
				day = date.getDate(),
				hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
				minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
				second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
			month >= 1 && month <= 9 ? (month = "0" + month) : "";
			day >= 0 && day <= 9 ? (day = "0" + day) : "";
			var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
			return timer;
		},

		toShopping() {
			if (this.SelectMenDian == '') {
				uni.showToast({
					title: "请先选择门店",
					icon: 'none'
				});
			} else {
				uni.navigateTo({
					url: "/pages/shopping/shopping?id=" + this.SelectMenDian,
				})
			}

		},
		toOrder() {
			uni.navigateTo({
				url: "/pages/order/newOrder"
			})
		},

	}
}